package com.zhengke.controller.sysmgr;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.ZTreeNode;
import com.zhengke.pojo.SysArea;
import com.zhengke.service.area.AreaService;

/**
 * 区域分级管理
 * @author zmk
 *
 */
@Controller
@RequestMapping("area")
public class AreaController {
	@Autowired
	AreaService service;
	@Autowired
	private CommonDao dao;
	
	/**
	 * 去区域分级管理页面
	 */
	@RequestMapping("toArea.do")
	public String toArea(){
		return "area/area";
	}
	/**
	 * 生成树
	 */
	@RequestMapping("tree.do")
	@ResponseBody
	public List<ZTreeNode> terr(String id) {
		List<ZTreeNode> list = new ArrayList<ZTreeNode>();
		if(id!=null) {
			list = service.querySub(id);
			return list;
		}
		list = service.queryParent();
		return list;
	}
	
	/**
	 * 添加根分类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addParent.do")
	public AjaxDone addParentClass() {
		SysArea area= new SysArea();
		area.setAreaName("新增分类");
		area.setAreaDeep("1");
		area.setAreaParentId(0);
		area.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		area.setCreateUserId(1);
		try{
			int rst = service.insert(area);
			if(rst>0)
				return new AjaxDone("200","添加父级分类成功!","",area);
		}catch(Exception e){
			e.printStackTrace();
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 添加子分类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addSubClass.do")
	public AjaxDone addSubClass(HttpServletResponse response,HttpServletRequest request) {
		String areaId =request.getParameter("areaId"); 
		String areaDeep=request.getParameter("areaDeep");
		int deep = Integer.valueOf(areaDeep);
		deep=deep+2;
		SysArea area= new SysArea();
		area.setAreaName("新增下级分类地区");
		area.setAreaDeep(String.valueOf(deep));
		area.setAreaParentId(Integer.valueOf(areaId));
		area.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		area.setAreaId(1);
		try{
			int rst = service.insert(area);
			if(rst>0)
				return new AjaxDone("200","添加子级分类成功!","",area);
		}catch(Exception e){
			e.printStackTrace();
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 去修改地区分级信息
	 */
	@RequestMapping("toAreaUpd.do")
	public String toAreaUpd(String areaId,ModelMap model) {
		SysArea sysArea= service.queryByAreaId(areaId);
		model.put("areaName", sysArea.getAreaName());
		model.put("areaCode", sysArea.getAreaCode());
		model.put("areaDescr", sysArea.getAreaDescr());
		model.put("areaId", areaId);
		if(sysArea.getAreaDeep().equals("1")) {
			model.put("pAreaName", "无");
			model.put("areaParentId", "0");
		}
		if(!sysArea.getAreaDeep().equals("1")) {
			SysArea area = service.queryByAreaId(sysArea.getAreaParentId().toString());
			model.put("pAreaName", area.getAreaName());
			model.put("areaParentId", area.getAreaParentId());
		}
		return "area/toAreaUpd";
	}
	/**
	 * 修改地区分级信息
	 */
	@ResponseBody
	@RequestMapping("updateAreaClass.do")
	public AjaxDone saveAreaClass(Integer areaId,String areaName,Integer areaParentId,String areaCode,String areaDescr) {
		SysArea sysArea= new SysArea();
		sysArea.setAreaId(areaId);
		sysArea.setAreaName(areaName);
		sysArea.setAreaParentId(areaParentId);
		sysArea.setAreaCode(areaCode);
		sysArea.setAreaDescr(areaDescr);
		int num = service.saveSysArea(sysArea);
		if(num>0) {
			Map<String,String> rtnMap = new HashMap<String,String>();
			rtnMap.put("areaName", areaName);
			return new AjaxDone("200", "保存成功!", "", rtnMap);
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 删除地区分级
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("delAreaClass.do")
	public AjaxDone delAreaClass(String areaId) {
		//查询所选的地区有没有下级地区
		try {
			List<SysArea> sysAreaList = (List<SysArea>) dao.findForList("areaDaoMapper.querySub", areaId);
			if(sysAreaList.size()>0) {
				return new AjaxDone("300","该商品分类有子分类,请先删除其子分类!");
			}else {
				int num = service.delAreaClass(areaId);
				if(num>=0) {
					return new AjaxDone("200","删除成功!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	
}
